class ToursController < ApplicationController
  
  layout 'admin'

  # GET /tours
  # GET /tours.json
  def index
    @tours = Tour.all

    respond_to do |format|
      format.js { render layout: false }
    end
  end

  # GET /tours/1
  # GET /tours/1.json
  def show
    @tour = Tour.find(params[:id])

    respond_to do |format|
      format.js { render layout: false }
    end
  end

  # GET /tours/new
  # GET /tours/new.json
  def new
    @tour = Tour.new

    respond_to do |format|
      format.js { render layout: false }
    end
  end

  # GET /tours/1/edit
  def edit
    @tour = Tour.find(params[:id])
  end

  # POST /tours
  # POST /tours.json
  def create
    @tour = Tour.new(params[:tour])
    @tour.save
    @tours = Tour.order(sort_column + ' ' + sort_direction).paginate(:per_page => 10, :page => params[:page])

    respond_to do |format|
      format.js { render layout: false }
    end
  end

  # PUT /tours/1
  # PUT /tours/1.json
  def update
    @tour = Tour.find(params[:id])
    @tour.update_attributes(params[:tour])
    @tours = Tour.order(sort_column + ' ' + sort_direction).paginate(:per_page => 10, :page => params[:page])

    respond_to do |format|
      format.js { render layout: false }
    end
  end

  # DELETE /tours/1
  # DELETE /tours/1.json
  def destroy
    @tour = Tour.find(params[:id])
    @tour.destroy
    @tours = Tour.order(sort_column + ' ' + sort_direction)
                         .paginate(:per_page => 10, :page => params[:page])

    respond_to do |format|
      format.js { render layout: false }
    end
  end

  private
    def sort_direction
      %w[asc desc].include?(params[:direction]) ?  params[:direction] : "asc"
    end

    def sort_column
      Hotel.column_names.include?(params[:sort]) || params[:sort] == 'tour_name' ? params[:sort] : "tour_name"
      Hotel.column_names.include?(params[:sort]) || params[:sort] == 'area_id' ? params[:sort] : "area_id"
    end

   def hotel_search
    if params[:search]
      session[:hotel_search] = params[:search]
    elsif params[:clear_session]
      session[:hotel_search] = nil
    end
    results = session[:hotel_search]
    results
  end
end
